package com.appspot.web2stanza;

import static com.google.appengine.api.urlfetch.FetchOptions.Builder.*;

import java.io.IOException;
import java.net.URL;

import com.google.appengine.api.urlfetch.*;

class GAEFetch {

  public static byte[] fetch(URL url)
      throws ResponseTooLargeException, IOException {
    URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
    HTTPRequest req = new HTTPRequest(
        url,
        HTTPMethod.GET,
        disallowTruncate().followRedirects().setDeadline(10.0));
    HTTPResponse res = urlFetchService.fetch(req);
    if (javax.servlet.http.HttpServletResponse.SC_NOT_FOUND == res.getResponseCode()) {
      String message = "Not Found: " + url.toString();
      MainServlet.log.warning("GAEFetch => " + message);
      throw new IOException(message);
    }
    return res.getContent();
  }

}
